<?php
/**
 * Created by PhpStorm.
 * User: dung
 * Date: 11/11/14
 * Time: 11:58 AM
 */

class Convert2Controller extends BaseController
{
    protected $db_forum = "vbbed";
    protected $db_media = "hocdan";
    protected $log;

    public function __construct()
    {
        $this->log = new Templog();
    }

    public function getUserVbb()
    {
        $connect = new Connect($this->db_forum);
        $skip = $this->log->getValue('User-Vbb');
        $result = $connect->QueryAll("SELECT * FROM user ORDER BY userid ASC LIMIT " . $skip . ",200");
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result)) {
                $user = User::getUserByUsername($row['username']);
                try {
                    if (!is_null($user)) {
                        $user->password = $row['password'];
                        $user->salt = $row['salt'];
                        $user->email = !empty($row['email']) ? $row['email'] : (Str::slug($row['username']) . $row['userid'] . "@adayroi.net");
                        $user->last_login = date('Y-m-d h:i:s', (!empty($row['lastvisit']) ? $row['lastvisit'] : time()));

                        $user->save();
                    }
                } catch
                (Exception $exx) {

                }
                $skip++;
            }

        } else
            return 'finish';
        $this->log->saveItem(array('code' => 'User-Vbb', 'value' => $skip));
        echo $skip;
    }

    public
    function getVCategory()
    {
        $source = new Connect($this->db_media);
        $skip = $this->log->getValue('vcategory');
        $result = $source->QueryAll("SELECT * FROM table_group");
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result)) {
                $vcategory = new Vcategory();
                $vcategory->id = $row['group_id'];
                $vcategory->title = $row['group_name'];
                $vcategory->slug = Str::slug($row['group_name']);
                $vcategory->status = ($row['group_status'] == 1 ? "on" : "off");
                $vcategory->save();
                $skip++;
            }
        }

        $this->log->saveItem(array('code' => 'vcategory', 'value' => $skip));
        echo 'finish';
    }

    public
    function getVideo()
    {
        $source = new Connect($this->db_media);
        $skip = $this->log->getValue('video');
        $result = $source->QueryAll("SELECT * FROM table_video where is_libs = 0 ORDER BY videoid ASC LIMIT " . $skip . ",100");
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result)) {
                $video = new Video();
                try {
                    $video->id = $row['videoid'];
                    $video->title = $row['videoname'];
                    $video->slug = Str::slug($row['videoname']);
                    $video->thumbnail = $row['img'];
                    $video->youtube = $row['videourl'];
                    $video->vcategory_id = !empty($row['groupm']) ? $row['groupm'] : 0;
                    $video->user_id = $row['ID_USER'];
                    $video->contest_id = !empty($row['contest_id']) ? $row['contest_id'] : 0;
                    $video->content = $row['lyrics'];
                    $video->views = $row['views'];
                    $video->votes = $row['votes'];
                    $video->favourites = $row['favourites'];
                    $video->status = $row['status'] == 1 ? "published" : "submitted";
                    $video->created_at = date('Y-m-d h:i:s', (!empty($row['postdate']) ? $row['postdate'] : time()));
                    $video->save();
                } catch (Exception $exx) {

                }
                $skip++;
            }
        } else
            return 'finish';
        $this->log->saveItem(array('code' => 'video', 'value' => $skip));
        echo $skip;
    }

    public
    function getUserStar()
    {
        $source = new Connect($this->db_media);
        $skip = $this->log->getValue('user-star');
        $result = $source->QueryAll("SELECT * FROM table_userstar");
        while ($row = mysqli_fetch_array($result)) {
            $userStar = new Userstar();
            $userStar->user_id = $row['id_user'];
            $userStar->video_id = $row['id_video'];
            $userStar->display_name = $row['s_content'];
            $userStar->content = $row['f_content'];
            $userStar->status = ($result['status'] == 1) ? "on" : "off";
            $userStar->created_at = date('Y-m-d h:i:s', $row['datepost']);
            $userStar->user_image = $row['su_image'];
            $userStar->save();
            $skip++;
        }
//        $skip = 'finish';
        $this->log->saveItem(array('code' => 'user-star', 'value' => $skip));
        return 'finish';
    }

    public
    function getEvent()
    {
        $source = new Connect($this->db_media);
        $rs = $source->QueryAll("SELECT * FROM table_event where e_is_contest = 1");
        while ($row = mysqli_fetch_array($rs)) {
            $contest = new Contest();
            $contest->id = $row['e_id'];
            $contest->title = $row['e_title'];
            $contest->slug = Str::slug($row['e_title']);
            $contest->start = $row['e_date_start'];
            $contest->end = $row['e_date_end'];
            $contest->description = $row['e_description'];
            $contest->status = $row['e_status'] == 1 ? "on" : "off";
            $contest->banner = $row['e_banner'];
            $contest->save();
        }
        $skip = 'finish';
        $this->log->saveItem(array('code' => 'event', 'value' => $skip));
        echo $skip;
    }

    public
    function getEventRef()
    {
        $source = new Connect($this->db_media);
        $rs = $source->QueryAll("SELECT * FROM table_event_navpages");
        while ($row = mysqli_fetch_array($rs)) {
            $contest = new Contest();
            $contest->title = $row['ep_title'];
            $contest->slug = Str::slug($row['ep_title']);
            $contest->parent_id = $row['ep_eventid'];
            $contest->description = $row['ep_content'];
            $contest->type = ($row['ep_type'] == 1 ? "about" : ($row['ep_type'] == 2 ? "guide" : "award"));
            $contest->save();
        }
        $skip = 'finish';
        $this->log->saveItem(array('code' => 'event-ref', 'value' => $skip));
        echo $skip;
    }

    public
    function getBlogCategory()
    {
        $source = new Connect($this->db_media);
        $rs = $source->QueryAll("SELECT * FROM table_tintuc Where parrent_id = 0");
        while ($row = mysqli_fetch_array($rs)) {
            $bCategory = new BCategory();
            $bCategory->id = $row['id'];
            $bCategory->title = $row['title'];
            $bCategory->slug = Str::slug($row['title']);
            $bCategory->parent_id = 0;
            $bCategory->status = "on";
            $bCategory->save();
        }
        $skip = 'finish';
        $this->log->saveItem(array('code' => 'blog-category', 'value' => $skip));
        echo $skip;
    }

    public
    function getBlog()
    {
        $source = new Connect($this->db_media);
        $skip = $this->log->getValue('blog');
        $rs = $source->QueryAll("SELECT * FROM table_tintuc Where parrent_id !=0 LIMIT " . $skip . ",100");
        if (mysqli_num_rows($rs) > 0) {
            while ($row = mysqli_fetch_array($rs)) {
                $blog = new Blog();
                $blog->id = $row['id'];
                $blog->title = $row['title'];
                $blog->slug = Str::slug($row['title']);
                $blog->ncategory_id = $row['parrent_id'];
                $blog->thumbnail = $row['img'];
                $blog->content = $row['content'];
                $blog->lead = $row['descr'];
                $blog->is_hot = $row['is_hot'] == 1 ? 1 : 0;
                $blog->status = $row['status'] == 1 ? 'on' : 'off';
                $blog->save();
                $skip++;
            }
        } else
            return 'finish';
        $this->log->saveItem(array('code' => 'blog', 'value' => $skip));
        echo $skip;
    }
} 